﻿using System.Windows.Forms;
using System.Data.Objects;
using System.Data.Common;
using System.Data;

namespace Northwind.NET.EDM.FE.WinForms.Controls
{
    public partial class SuppliersUserControl : BaseUserControl
    {
        protected override void OnLoad(System.EventArgs e)
        {
            base.OnLoad(e);
            LoadSuppliers();
        }

        private void LoadSuppliers()
        {
            using (DbConnection connection = Factory.CreateConnection())
            {
                connection.ConnectionString = ConnectionString;
                DbDataAdapter adapter = Factory.CreateDataAdapter();
                DbCommand command = Factory.CreateCommand();
                command.Connection = connection;
                command.CommandText = "SELECT ID, Name, ContactName, " +
                                        "ContactTitle, Address, City, Region, " +
                                        "PostalCode, Country, Phone, Fax, " + 
                                        "HomePage FROM Suppliers";

                adapter.SelectCommand = command;

                DataSet dataset = new DataSet();
                dataset.Tables.Add("Suppliers");
                adapter.Fill(dataset.Tables["Suppliers"]);

                this.supplierBindingSource.DataSource = dataset.Tables["Suppliers"];
            }
        }
        public SuppliersUserControl()
        {
            InitializeComponent();
        }

        public override string FormCaption { get { return Settings.Resources.SuppliersFormCaption; } }
        public override dynamic GetEntitySet(ObjectContext context)
        {
            return new Model.Supplier();
        }

        public override BindingSource UserControlBindingSource { get { return supplierBindingSource; }}

    }
}
